package org.hot100_1;

import org.algorithm.List.ListNode;

public class leetcode29 {
    public static ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode dummy = new ListNode(-1);
        dummy.next=head;

        ListNode fast = dummy;
        ListNode slow = dummy;

        for (int i = 0; i <=n; i++) {
            fast = fast.next;
        }

        while (fast!=null){
            fast=fast.next;
            slow=slow.next;
        }

        slow.next=slow.next.next;

        return dummy.next;
    }

    public static void main(String[] args) {
        ListNode test = new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5,null)))));
        ListNode listNode = removeNthFromEnd(test, 2);
        System.out.println(listNode.val);
    }
}
